import { defineStore } from 'pinia'
import * as departmentApi from '@/api/department'

export const useDepartmentStore = defineStore('department', {
  state: () => ({
    // 部门列表
    departmentList: [],
    // 总记录数
    total: 0,
    // 加载状态
    loading: false,
    // 错误信息
    error: null
  }),

  getters: {
    // 获取部门列表
    getDepartments: (state) => state.departmentList,
    // 获取总数
    getTotal: (state) => state.total,
    // 获取加载状态
    isLoading: (state) => state.loading,
    // 获取部门选项（用于下拉选择）
    getDepartmentOptions: (state) => {
      return state.departmentList
        .filter(dept => dept.status === 1) // 只返回正常状态的部门
        .map(dept => ({
          id: dept.id,
          label: dept.name,
          value: dept.id
        }))
    }
  },

  actions: {
    /**
     * 加载部门列表
     * @param {Object} params 查询参数
     */
    async loadDepartmentList(params = {}) {
      this.loading = true
      this.error = null
      try {
        const response = await departmentApi.getDepartmentList(params)
        if (response.code === 1) {
          this.departmentList = response.data.rows || []
          this.total = response.data.total || 0
        }
      } catch (error) {
        this.error = error.message
      } finally {
        this.loading = false
      }
    },

    /**
     * 获取所有部门
     */
    async loadAllDepartments() {
      this.loading = true
      this.error = null
      try {
        const response = await departmentApi.getAllDepartments()
        if (response.code === 1) {
          this.departmentList = response.data || []
        }
      } catch (error) {
        this.error = error.message
      } finally {
        this.loading = false
      }
    },

    /**
     * 添加部门
     * @param {Object} departmentData 部门数据
     * @returns {Promise} 添加结果
     */
    async addDepartment(departmentData) {
      this.loading = true
      this.error = null
      try {
        const response = await departmentApi.addDepartment(departmentData)
        if (response.code === 1) {
          // 添加成功后重新加载列表
          await this.loadDepartmentList()
          return { success: true }
        }
        return { success: false, message: response.msg }
      } catch (error) {
        this.error = error.message
        return { success: false, message: error.message }
      } finally {
        this.loading = false
      }
    },

    /**
     * 更新部门
     * @param {Object} departmentData 部门数据
     * @returns {Promise} 更新结果
     */
    async updateDepartment(departmentData) {
      this.loading = true
      this.error = null
      try {
        const response = await departmentApi.updateDepartment(departmentData)
        if (response.code === 1) {
          // 更新成功后重新加载列表
          await this.loadDepartmentList()
          return { success: true }
        }
        return { success: false, message: response.msg }
      } catch (error) {
        this.error = error.message
        return { success: false, message: error.message }
      } finally {
        this.loading = false
      }
    },

    /**
     * 删除部门
     * @param {number} id 部门ID
     * @returns {Promise} 删除结果
     */
    async deleteDepartment(id) {
      this.loading = true
      this.error = null
      try {
        const response = await departmentApi.deleteDepartment(id)
        if (response.code === 1) {
          // 删除成功后重新加载列表
          await this.loadDepartmentList()
          return { success: true }
        }
        return { success: false, message: response.msg }
      } catch (error) {
        this.error = error.message
        return { success: false, message: error.message }
      } finally {
        this.loading = false
      }
    },

    /**
     * 重置状态
     */
    resetState() {
      this.departmentList = []
      this.total = 0
      this.loading = false
      this.error = null
    }
  }
})